use tauri::{
    WindowEvent,
    Window,
};

mod commands;
mod db;
mod tray;


#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
    .plugin(tauri_plugin_opener::init())
    .setup(|app| {
        if cfg!(debug_assertions) {
            app.handle().plugin(
                tauri_plugin_log::Builder::default().level(log::LevelFilter::Info).build(),
            )?;
        }

        // Gray
        tray::set_tray(app).unwrap();

        // Database
        db::init_db_path(app);
        commands::init_tables().unwrap();

        Ok(())
    })
    .on_window_event(|window: &Window, _event: &WindowEvent| {
        if window.label() == "main" {
            match _event {
                WindowEvent::CloseRequested { api, .. } => {
                    api.prevent_close();
                    window.hide().unwrap();
                },
                _ => {}
            }
        }
    })
    .invoke_handler(tauri::generate_handler![
        commands::init_tables,

        commands::get_company_all,
        commands::get_company,
        commands::add_company,
        commands::delete_company,
        commands::update_company,

        commands::get_project_all,
        commands::get_project,
        commands::add_project,
        commands::delete_project,
        commands::update_project,

        commands::get_item_all,
        commands::get_item,
        commands::add_item,
        commands::delete_item,
        commands::update_item,

        commands::get_bookmark_all,
        commands::get_bookmark,
        commands::add_bookmark,
        commands::delete_bookmark,
        commands::update_bookmark,

        commands::get_user,
        commands::add_user,
        commands::update_user,
        commands::add_or_update_user,

        commands::open_link,
        commands::get_config,
    ])
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}
